FROM python:3.12.0-slim

WORKDIR /app

# 设置Python环境变量
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
# 添加uvicorn配置环境变量
ENV UVICORN_WORKERS=4
ENV UVICORN_THREADS=2

# 复制依赖文件
COPY pyproject.toml uv.lock ./

# 安装系统依赖和Python依赖
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc \
    && rm -rf /var/lib/apt/lists/* \
    && pip install --no-cache-dir -U pip uv \
    && uv sync --no-cache --system .[dev] \
    && apt-get purge -y --auto-remove gcc

# 复制项目文件
COPY . .

# 暴露端口
EXPOSE 8005

# 添加健康检查
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8005/health || exit 1

# 创建非root用户
RUN adduser --disabled-password --gecos '' appuser \
    && chown -R appuser:appuser /app
USER appuser

# 启动命令
CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port 8005 --workers $UVICORN_WORKERS --threads $UVICORN_THREADS"]